package com.resys.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.resys.entity.po.Customer;
import com.resys.entity.po.Reservationorder;
import com.resys.entity.po.User;
import com.resys.entity.query.*;
import com.resys.exception.BusinessException;
import com.resys.mappers.CustomerMapper;
import com.resys.mappers.ReservationorderMapper;
import com.resys.mappers.UserMapper;
import org.springframework.stereotype.Service;

import com.resys.entity.enums.PageSize;
import com.resys.entity.po.Comment;
import com.resys.entity.vo.PaginationResultVO;
import com.resys.mappers.CommentMapper;
import com.resys.service.CommentService;
import com.resys.utils.StringTools;


/**
 * 业务接口实现
 */
@Service("commentService")
public class CommentServiceImpl implements CommentService {

    @Resource
    private CommentMapper<Comment, CommentQuery> commentMapper;
    @Resource
    private CustomerMapper<Customer, CustomerQuery> customerMapper;
    @Resource
    private UserMapper<User, UserQuery> userMapper;
    @Resource
    private ReservationorderMapper<Reservationorder, ReservationorderQuery> reservationorderMapper;

    /**
     * 根据条件查询列表
     */
    @Override
    public List<Comment> findListByParam(CommentQuery param) {
        return this.commentMapper.selectList(param);
    }

    /**
     * 根据条件查询列表
     */
    @Override
    public Integer findCountByParam(CommentQuery param) {
        return this.commentMapper.selectCount(param);
    }

    /**
     * 分页查询方法
     */
    @Override
    public PaginationResultVO<Comment> findListByPage(CommentQuery param) {
        int count = this.findCountByParam(param);
        int pageSize = param.getPageSize() == null ? PageSize.SIZE15.getSize() : param.getPageSize();

        SimplePage page = new SimplePage(param.getPageNo(), count, pageSize);
        param.setSimplePage(page);
        List<Comment> list = this.findListByParam(param);
        PaginationResultVO<Comment> result = new PaginationResultVO(count, page.getPageSize(), page.getPageNo(), page.getPageTotal(), list);
        return result;
    }

    /**
     * 新增
     */
    @Override
    public Integer add(Comment bean) {
        return this.commentMapper.insert(bean);
    }

    /**
     * 批量新增
     */
    @Override
    public Integer addBatch(List<Comment> listBean) {
        if (listBean == null || listBean.isEmpty()) {
            return 0;
        }
        return this.commentMapper.insertBatch(listBean);
    }

    /**
     * 批量新增或者修改
     */
    @Override
    public Integer addOrUpdateBatch(List<Comment> listBean) {
        if (listBean == null || listBean.isEmpty()) {
            return 0;
        }
        return this.commentMapper.insertOrUpdateBatch(listBean);
    }

    /**
     * 多条件更新
     */
    @Override
    public Integer updateByParam(Comment bean, CommentQuery param) {
        StringTools.checkParam(param);
        return this.commentMapper.updateByParam(bean, param);
    }

    /**
     * 多条件删除
     */
    @Override
    public Integer deleteByParam(CommentQuery param) {
        StringTools.checkParam(param);
        return this.commentMapper.deleteByParam(param);
    }

    /**
     * 根据CommentId获取对象
     */
    @Override
    public Comment getCommentByCommentId(String commentId) {
        return this.commentMapper.selectByCommentId(commentId);
    }

    /**
     * 根据CommentId修改
     */
    @Override
    public Integer updateCommentByCommentId(Comment bean, String commentId) {
        return this.commentMapper.updateByCommentId(bean, commentId);
    }

    /**
     * 根据CommentId删除
     */
    @Override
    public Integer deleteCommentByCommentId(String commentId) {
        return this.commentMapper.deleteByCommentId(commentId);
    }

    @Override
    public void commentsubmit(String content, String customerId, String commentId, String replyId, Integer role) {
        Comment commentreply = this.commentMapper.selectByCommentId(replyId);
        Comment comment = new Comment();
        comment.setContent(content);
        comment.setCommitId(customerId);
        Customer customer = customerMapper.selectByCustomerId(customerId);
        User user = userMapper.selectByUserId(customerId);
        if (role == 1&& customer != null) {

                comment.setCommitName(customer.getClientName());

        } else if(user!= null){
                comment.setCommitName(user.getUsername());
        }
        comment.setCommitRole(role);
        comment.setType(1);
        comment.setCreateAt(new Date());
        comment.setReplyer(replyId);
        comment.setIsTop(0);
        comment.setCommentpid(commentId);
        Comment pid = this.commentMapper.selectByCommentId(commentId);
        if (pid != null) {
            comment.setRoomId(pid.getRoomId());
        }
        if (commentreply != null) {
            comment.setReplyerName(commentreply.getCommitName());
        }
        comment.setCommentId(StringTools.getRandomString(10));
        this.commentMapper.insert(comment);
    }

    @Override
    public void commentadd(String content, String customerId, Integer likesCount, Integer orderId, Integer role) {
        CommentQuery qu = new CommentQuery();
        qu.setType(0);
        qu.setOrderId(orderId);
        qu.setCommitId(customerId);
        List<Comment> list = this.commentMapper.selectList(qu);
        if (list != null&& list.size() > 0) {
            throw new BusinessException("请勿重复评价");
        }
        Comment comment = new Comment();
        comment.setCommentId(StringTools.getRandomString(10));
        comment.setCreateAt(new Date());
        comment.setIsTop(0);
        comment.setContent(content);
        comment.setLikesCount(likesCount);
        comment.setType(0);
        comment.setOrderId(orderId);
        comment.setCommitId(customerId);
        if (role == 1) {
            Customer customer = customerMapper.selectByCustomerId(customerId);
            if (customer != null) {
                comment.setCommitName(customer.getClientName());
            }
        } else {
            User user = userMapper.selectByUserId(customerId);
            if (user != null) {
                comment.setCommitName(user.getUsername());
            }
        }
        comment.setCommitRole(role);
        Reservationorder order = reservationorderMapper.selectByOrderId(orderId);
        if (order != null) {
            comment.setRoomId(order.getRoomId());
            comment.setUseAt(order.getEndTime());
        }
        this.commentMapper.insert(comment);
    }

    @Override
    public void del(String commentId) {
        Comment comment = this.commentMapper.selectByCommentId(commentId);
        if (comment == null) {
            throw new RuntimeException("评论不存在");
        }
        if(comment.getType()==0){
            CommentQuery param = new CommentQuery();
            param.setCommentpid(commentId);
            param.setType(1);
            this.commentMapper.deleteByParam(param);
        }
        this.commentMapper.deleteByCommentId(commentId);
    }

}